var useable = true;
function GenarateFolio()
{
	if($("[id*=edit_res_detail_guestname_][guestid=0]").not($("[onclick]")).size() ==0){
	var chargeArray  = new Array();
	var cArray  = new Array();
	var chargeResArray= new Array();
	var cResArray= [];
	var chargeGroupArray= new Array();
	$("input[name*=checkbox_charge_res]:visible").each(function() {
		chargeResArray.push($(this).attr("resId"));
		});
	$.each(chargeResArray, function(i, el){
	    if($.inArray(el, cResArray) === -1) cResArray.push(el);
	});

	for(var t = 0;t<cResArray.length;t++)
	{
		$("input[name*=checkbox_charge_res][resid="+cResArray[t]+"]:visible").each(function() {
		
			chargeArray.push( $(this).attr("value"));
			
			});
		if(chargeArray.length == 1)
		{
			cArray.push(chargeArray);
			chargeArray=[];
		}
		else
		{
		var varText =chargeArray.toString().split(",");
		var charText="";
		for(var a=0;a<varText.length;a++)
		{
			if(a == (varText.length-1))
			{
				charText+=varText[a];
			}else
			{
				charText+=varText[a]+"_";
			}
		}
		cArray.push(charText);
		chargeArray=[];
		}
	}
	$("input[name*=checkbox_charge_group]:visible").each(function() {
			chargeGroupArray.push($(this).attr("value"));		
		});
	var chargeGroupId = $("[name*=checkbox_charge_group]:visible").first().attr("resid");
	if(chargeGroupId == null)
	{
		chargeGroupId ="0";
	}
	var url = "/folioService?func=addFolio&chargeres="+cArray+"&resId="+cResArray+"&chargegroup="+chargeGroupArray+"&chargegid="+chargeGroupId;
	simpleserviceWithArgs(url,null,GenarateFolioSuccess,null);
	}
	else
	{
		simpleDialog("Insert guest !!",100,null);
	}
}

function GenarateFolioSuccess(data,obj)
{
	var objArray = JSON.parse(data);
	var checkLogOut = CheckSession(objArray);
	if(checkLogOut)
		return;
	if(objArray  == null){
		simpleDialog(saveFail_label,100,null);
	}
	else
	{
		var Tabs = getCurrentTabData();
		Tabs.FolioList=objArray;
		if(Tabs.type == "Single")
		{
			var url="/folioService?func=loadSingleFolioList&resId="+Tabs.resId;
			simpleserviceWithArgs(url,null,loadFolioSuccess,null);
		}else
		{
			ShowFolio();
		}
	}
}

function loadFolioSuccess(data,obj)
{
	var Tabs = getCurrentTabData();
	var objArray = JSON.parse(data);
	Tabs.FolioList = objArray;
	fillFolio();
}
function ShowFolio(FloAry)
{
	var guest =guestLag;
	var amount =amountLag;
	var paid =paidLag;
	var balance=balanceLag;
	var text="";
	var textOwnerPayment="";
	var Tabs = getCurrentTabData();
			var objArray = Tabs.FolioList;
			if(FloAry!=null){objArray = FloAry;}
			if(objArray != null)
			{
				
				$("[id*=route-to-folio-item]").find($("div[folioId]")).remove();
				
				$("#lender-folio-list_0").attr("id","lender-folio-list");
				$("[id*=lender-folio-list_]").remove();
				
				$("#payment-folio-list-generate-foliochb").css("display","");
				$("#payment-folio-list-footer").css("display","");
				$("#payment-no-folio-list").hide();
				
				$("#payment-folio-title").replaceWith(
				"<tr id =\"payment-folio-title\">"+
				"<td colspan=\"1\" width=\"20%\"><input type=\"checkbox\" id=\"payment-folio-master-chekbox\" onclick=\"Allcheck()\"></td>"+
				"<td colspan=\"2\" width=\"45%\">"+guest+"</td>"+
				"<td colspan=\"1\" width=\"13%\">"+amount+"</td>"+
				"<td colspan=\"1\" width=\"6%\">"+paid+"</td>"+
				"<td colspan=\"1\" width=\"11%\">"+balance+"</td>"+
				"<td colspan=\"1\" width=\"5%\">&nbsp;</td>"+
				"</tr>");

				for(var y=0;y<objArray.length;y++)
				{
					
					for(var n=0;n<objArray[y][1].length;n++)
					{
				
						if(objArray[y][1][n]['roomName']=="owner")
						{
							text+="<tr id=\"lender-folio-list_"+n+"\">"+
							"<td colspan=\"7\" width=\"100%\" style=\"background-color:#8F8FE7; padding-left:10px;\">"+
							"<span id=\"payment-folio-groupowner\" style=\"color:#000;\"><b>"+ownerGroupLag+"</b></span>"+
							"</td>"+
							"</tr>";	
						}else
						{
							 text+="<tr id=\"lender-folio-list_"+n+"\">"+
								"<td colspan=\"7\" width=\"100%\" style=\"background-color:#8F8FE7; padding-left:10px;\">"+
								"<strong><span>"+(n+1)+" </span><span>"+roomtypeLag+":</span>"+
								"<span id=\"payment-folio-roomtype\" style=\"color:#103083;\">"+objArray[y][1][n]['roomType']+"</span>"+
								"<span id=\"payment-folio-roomname\" style=\"color:#103083;\">("+objArray[y][1][n]['roomName']+")</span></strong>"+
								"</td>"+
								"</tr>";
						}
			 
					
					for(var i=0;i<objArray[y][0].length;i++)
					{
						var paids =objArray[y][0][i]['paid'];
						if(paids == 0 || paids==null )
						{
							paids ="-";
						}
						if(objArray[y][0][i]['resId'] != null)
						{
							text+="<tr style=\"background-color:#FFF;\">"+
							"<td colspan=\"3\" >"+
							"<table width=\"100%\" style=\"color:#FFF;\" >"+
							"<tr class=\"payment-hover-folioitem\" type=\"singlePaymentFolioIngroup\" onclick=\" if (useable) LoadFolioPage(this)\" >"+
							"<td colspan=\"1\" width=\"20%\" onmouseover=\"useable=false;\" onmouseout=\"useable=true;\"><input type=\"checkbox\" name=\"payment-folio-chekbox-res\" resId=\""+objArray[y][0][i]['resId']+"\" folioId=\""+objArray[y][0][i]['folioId']+"\" ><span id=\"payment-folio-subfolio\" style=\"color:#103083; padding-left:20px;\"  >"+"PI "+objArray[y][0][i]['folioId']+"</span> </td>"+
							"<td colspan=\"2\" width=\"45%\"><span id=\"payment-folio-guestname\" style=\"color:#999\">"+objArray[y][0][i]['guestName']+"</span>&nbsp;<span id=\"payment-folio-guestid\" style=\"color:#999\">"+"(P"+objArray[y][0][i]['guestId']+")"+"</span></td>"+
							"<td colspan=\"1\" width=\"13%\"><span id=\"payment-folio-moneyunit\" style=\"color:#999\">(฿)</span>&nbsp;<span name=\"payment-folio-money-amount\" style=\"color:#999\">"+objArray[y][0][i]['money']+"</span></td>"+
							"<td colspan=\"1\" width=\"6%\"><span id=\"payment-folio-paid\" style=\"color:#999\">"+paids+"</span></td>"+
							"<td colspan=\"1\" width=\"11%\"><span id=\"payment-folio-moneyunit\" style=\"color:#999\">(฿)</span>&nbsp;<span id=\"payment-folio-balance\" style=\"color:#999\">"+objArray[y][0][i]['balance']+"</span></td>"+
							"<td colspan=\"1\" width=\"5%\"><img src=\"/images/components/payment/unlock_icon.png\" width=\"20px\" height=\"20px\" islong="+objArray[y][0][i]['isLock']+"></td>"+
							"</tr>"+
							"</table>"+
							"</td>"+
							"</tr>";
							
							textOwnerPayment+="<div resid=\""+objArray[y][0][i]['resId']+"\" folioId=\""+objArray[y][0][i]['folioId']+"\" balance=\""+objArray[y][0][i]['balance']+"\" class=\"route-payment-hover\" onclick=\"GetOwnerofPayment(this)\">"+
							"<span>"+objArray[y][0][i]['roomType']+"("+objArray[y][0][i]['roomName']+")("+objArray[y][0][i]['folioId']+")"+"</span></div>";	
							$("#route-to-folio-item").append( 
									"<div folioId=\""+objArray[y][0][i]['folioId']+"\" balance=\""+objArray[y][0][i]['balance']+"\" class=\"route-payment-hover\" onclick=\"GetFolioOfcharge(this)\">"+
									"<span>"+objArray[y][0][i]['guestName']+" ("+objArray[y][0][i]['folioId']+")"+"</span></div>");	
						}else
						{
							var icon ="";
							(objArray[y][0][i]['isLock'])? icon=" iconlockedpocition":icon =" iconunlockpocition";
							text+="<tr style=\"background-color:#FFF;\">"+
							"<td colspan=\"3\" >"+
							"<table width=\"100%\" style=\"color:#FFF;\" >"+
							"<tr class=\"payment-hover-folioitem\" type=\"paymentgroupFolio\"  onclick=\" if (useable) LoadFolioPage(this)\" >"+
							"<td colspan=\"1\" width=\"20%\" onmouseover=\"useable=false;\" onmouseout=\"useable=true;\"><input type=\"checkbox\" name=\"payment-folio-chekbox-group\" groupId=\""+objArray[y][0][i]['gId']+"\" folioId=\""+objArray[y][0][i]['folioId']+"\">" +
							"<span id=\"payment-folio-subfolio\" style=\"color:#103083; padding-left:20px;\">"+"PI "+objArray[y][0][i]['folioId']+"</span> </td>"+
							"<td colspan=\"2\" width=\"45%\"><span id=\"payment-folio-guestname\" style=\"color:#999\">"+objArray[y][0][i]['guestName']+"</span>&nbsp;<span id=\"payment-folio-guestid\" style=\"color:#999\">"+"(P"+objArray[y][0][i]['guestId']+")"+"</span></td>"+
							"<td colspan=\"1\" width=\"13%\"><span id=\"payment-folio-moneyunit\" style=\"color:#999\">(฿)</span>&nbsp;<span name=\"payment-folio-money-amount\" style=\"color:#999\">"+objArray[y][0][i]['money']+"</span></td>"+
							"<td colspan=\"1\" width=\"6%\"><span id=\"payment-folio-paid\" style=\"color:#999\">"+paids+"</span></td>"+
							"<td colspan=\"1\" width=\"11%\"><span id=\"payment-folio-moneyunit\" style=\"color:#999\">(฿)</span>&nbsp;<span id=\"payment-folio-balance\" style=\"color:#999\">"+objArray[y][0][i]['balance']+"</span></td>"+
							"<td colspan=\"1\" width=\"5%\"><div class=\"iconlockandunlock "+icon+"\" onclick=\"lockFolio(this)\" islong="+objArray[y][0][i]['isLock']+"></div></td>"+
							"</tr>"+
							"</table>"+
							"</td>"+
							"</tr>";
						
							
						textOwnerPayment+="<div gId=\""+objArray[y][0][i]['gId']+"\" folioId=\""+objArray[y][0][i]['folioId']+"\" balance=\""+objArray[y][0][i]['balance']+"\"  class=\"route-payment-hover\" onclick=\"GetOwnerofPayment(this)\">"+
						"<span>"+ownerGroupLag+"("+objArray[y][0][i]['folioId']+")"+"</span></div>";
						$("#route-to-folio-item").append( 
								"<div folioId=\""+objArray[y][0][i]['folioId']+"\" balance=\""+objArray[y][0][i]['balance']+"\" class=\"route-payment-hover\" onclick=\"GetFolioOfcharge(this)\">"+
								"<span>"+objArray[y][0][i]['guestName']+" ("+objArray[y][0][i]['folioId']+")"+"</span></div>");	
						 }
					
				}
				}
					
				}
				
				$("input[name=payment-folio-chekbox-res]").parent().parent().parent().parent().parent().parent().remove();
				$("input[name=payment-folio-chekbox-group]").parent().parent().parent().parent().parent().parent().remove();
				
				$("[id=route-payment-to]").children().remove();
				if($("#route-payment-to").children().size()<objArray.length)
				{
				$("#route-payment-to").append(textOwnerPayment);
				}
				textOwnerPayment="";
				
				$("#lender-folio-list").replaceWith(text);
				var Amount =0;
				for(var x =0;x<$("[name=payment-folio-money-amount]").size();x++)
				{
				Amount+=parseInt($("[name=payment-folio-money-amount]")[x].innerHTML);
				}
				$("#payment-folio-btn-total-amount").text(Amount);
			}
			else
			{
				$("#payment-folio-title").replaceWith(
						"<tr id =\"payment-folio-title\">"+
						"<td width=\"100%\">&nbsp;</td>"+
						"</tr>");
				$("[id*=lender-folio-list]").hide();
				$("input[name=payment-folio-chekbox-res]").parent().parent().parent().parent().parent().parent().remove();
				$("#payment-folio-list-footer").css("display","none");
				$("#payment-no-folio-list").show();
			}
}

function DeleteFolio()  
{
	var exceptTexDialog='<div id="exceptTex_Dialog">'+delFolio+'</div>'; 
	 var buttons = [
	    			{ title:btnrutechargeOk , callback: function(thisdialog,args){
	    				DeleteFolioList();
	    				thisdialog.dialog("close");
	    			}},
	    		    { title:btnCancel , callback: function(thisdialog,args){
	    		    	thisdialog.dialog("close");
	    		    }}
	    		];
	    		customDialog(delFolio,exceptTexDialog,{ width: 389},buttons,null);
}

function DeleteFolioList()
{
	var folioIdArray  = new Array();
	var folioResIdArray= new Array();
	var folioIdGArray  = new Array();
	$("input[name=payment-folio-chekbox-group]:visible:checked").each(function() {
		
		folioIdGArray.push($(this).attr("folioid"));
		
		});
	
	
	var folioGroupId = $("[name=payment-folio-chekbox-group]:visible").first().attr("groupid");
	if(folioGroupId == null)
	{
		folioGroupId=0;
	}
	
	$("[name=payment-folio-chekbox-res]:visible:checked").each(function() {
		
		folioIdArray.push($(this).attr("folioId"));
		folioResIdArray.push($(this).attr("resId"));
		
		});
	
var url = "/folioService?func=deleteFolioList&resFolioList="+folioIdArray+"&resid="+folioResIdArray+"&gid="+folioGroupId+"&groupFolioList="+folioIdGArray;
simpleserviceWithArgs(url,null,DeleteFolioSuccess,null);

}

function DeleteFolioSuccess(data,obj)
{
	
	var objArray = new Array();
	objArray = JSON.parse(data);
	if(objArray  == false){
		simpleDialog(saveFail_label,100,null);
	}
	else
	{
		var checkLogOut =  CheckSession(objArray);
		if(checkLogOut)
			return;
		var Tabs = getCurrentTabData();
		if(Tabs.type == "Single")
		{
			Tabs.FolioList=objArray;
			var url="/folioService?func=loadSingleFolioList&resId="+Tabs.resId;
			simpleserviceWithArgs(url,null,loadFolioSuccess,null);
		}else
		{
			ShowFolio();
		}
	}
}

function Allcheck()
{
	
	if($("#payment-folio-master-chekbox").is(":checked")==true)
	{
		$("[name*=payment-folio-chekbox]").each(function() {
			
			$("[name*=payment-folio-chekbox]").prop('checked', true);
				
				});
		
	}else
		
	{
		
$("[name*=payment-folio-chekbox]").each(function() {
			
			$("[name*=payment-folio-chekbox]").prop('checked', false);
				
				});
	}
}
function lockFolio(e)
{
	var FolioId = $(e).parents('tr[type]').children('td:first').find('input').attr('folioid');
	var url = "/folioService?func=LockTheFolio&folioId="+FolioId;
	simpleserviceWithArgs(url,null,LockFolioSuccess,e);
}
function LockFolioSuccess(data,obj){
	
	var Tabs = getCurrentTabData();
	var arrayObj = JSON.parse(data);
	if(arrayObj == null){
		simpleDialog("Can't lock the Folio !",100,null);
	}else{
		var folioId =arrayObj.folioId;
		for(var i=0;i<Tabs.FolioList.length;i++){
			getListFromDataIndex(Tabs.FolioList[n][0],"folioId",folioId)[0].isLock=true;
		}
		var chargeListId =arrayObj.chargeListId.split(',');
		for(var i=0;i<Tabs.chargeListId.length;i++){
			getListFromDataIndex(Tabs.ChargeList,"id",chargeListId[i]).isLock=true;
		}
		$(obj).attr("class","iconlockandunlock iconlockedpocition");
		$(obj).attr("islong",true);
	}
}